<template>
  <div>
    <el-card shadow="false" class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="add" type="primary">新增</el-button>
      </div>
      <div class="text item">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column align="center" type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column
            align="center"
            prop="addressName"
            label="地址名称"
          ></el-table-column>
          <el-table-column align="center" prop="" label="默认发货地址">
            <template slot-scope="scope">
              <span>默认发货地址 </span
              ><el-switch
                v-model="scope.row.sendStatus"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#999"
                @change="setDefault(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="name" label="收货人姓名">
          </el-table-column>
          <el-table-column align="center" prop="phone" label="收货人电话">
          </el-table-column>
          <el-table-column align="center" prop="" label="收货人地址">
            <template slot-scope="scope">
              <div>
                <span
                  >{{ scope.row.province }} {{ scope.row.city }}
                  {{ scope.row.region }}</span
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="phone" label="默认收货地址">
            <template slot-scope="scope">
              <span>默认收货地址 </span
              ><el-switch
                v-model="scope.row.receiveStatus"
                active-color="#13ce66"
                inactive-color="#999"
                :active-value="1"
                :inactive-value="0"
                @change="getDefault(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column align="center" prop="" label="操作">
            <template slot-scope="scope">
              <el-button @click="edit(scope.row)" type="primary"
                >修改</el-button
              >
              <el-button @click="del(scope.row.id)" type="danger"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <addressCon :sonData="toson" ref="son" @tofather="getson"></addressCon>
  </div>
</template>

<script>
import addressApi from "@/api/order/address/index";
import addressCon from "@/components/order/address/index";
export default {
  components: { addressCon },

  data() {
    return {
      tableData: [],
      toson: {},
    };
  },

  created() {
    this.init();
  },

  mounted() {},

  methods: {
    // 设为收货默认
    getDefault(item) {
      console.log(item);
      var status = !item.receiveStatus;
      addressApi
        .setReceiveOne({
          id: item.id,
        })
        .then((res) => {
          console.log(res);
          if (res.success) {
            this.$message({
              message: "切换成功",
              type: "success",
            });
            this.init();
          } else {
            this.$message.error(res.message);
            item.receiveStatus = status;
          }
        });
    },
    // 设为发货默认
    setDefault(item) {
      console.log(item);
      var status = !item.sendStatus;
      addressApi
        .setSendOne({
          id: item.id,
        })
        .then((res) => {
          console.log(res);
          if (res.success) {
            this.$message({
              message: "切换成功",
              type: "success",
            });
            this.init();
          } else {
            item.sendStatus = status;
            this.$message.error(res.message);
          }
        });
    },
    // 编辑
    edit(item) {
      this.toson = item;
      // console.log(item);
      this.$refs.son.change();
    },
    // 删除
    del(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        addressApi.deleteAddress(id).then((res) => {
          console.log(res);
          if (res.success) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.init();
          } else {
            this.$message.error(res.message);
          }
        });
      });
    },
    // 子组件传过来的
    getson(ss) {
      console.log(ss);
      if (ss) {
        this.init();
      }
    },
    // 点击新增
    add() {
      this.toson = false;
      this.$refs.son.change1();
    },
    // 获取广告列表
    init() {
      addressApi.addressList().then((res) => {
        console.log(res);
        //  res.data.items.forEach(el=>{
        //    el
        //  })
        this.tableData = res.data.items;
      });
    },
  },

  computed: {},

  watch: {},
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 97%;
  margin: 15px auto;
}
</style>